/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'article',
                condition: {status: 'all','site_id':0},
                model: {
                    url:_global.url.api + 'article',
                    event: {
                        '.item-remove:click': 'remove',
                        '.edit_listorder:change': 'setlistorder'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setlistorder:function(e){
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if(new Model({
                                url: _global.url.api + 'article_set',
                                data:{id:id,listorder:listorder}
                            }).put()) {
                            _this.collection.fetch();
                        };
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.status =$("#status").val();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            $('#status').on('change', function(e) {
                e.preventDefault();
                _this.collection.condition.status =$("#status").val();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

        }
    };

    new index();

});